package com.atguigu.tingshu.order.strategy;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderDerateService;
import com.atguigu.tingshu.order.service.OrderDetailService;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import org.redisson.api.RBlockingDeque;
import org.redisson.api.RDelayedQueue;
import org.redisson.api.RedissonClient;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * Author  王东剑
 * Created on 23/6/2024 PM1:57
 * Package_name com.atguigu.tingshu.order.strategy
 * Description
 */
public interface SaveOrderStrategy {
	/**
	 * 保存类的接口
	 * @param orderInfoVo 参数
	 * @return 返回值
	 */
	OrderInfoVo saveOrder(OrderInfoVo orderInfoVo) ;

	/**
	 * 定义获取支付方式的方法
	 * @return 是被每个类的标识
	 */
	public abstract String getPayWay() ;

	/**
	 * 1.保存订单信息
	 * 2.保存订单明细
	 * 3.保存订单折扣
	 *
	 * @param orderInfoVo
	 */
	default void saveOrderInfoAndOrderDetailAndOrderDerate(OrderInfoVo orderInfoVo,
														   OrderInfoService orderInfoService,
														   OrderDetailService orderDetailService,
														   OrderDerateService orderDerateService) {
		Long userId = AuthContextHolder.getUserId();
		List<OrderDetailVo> orderDetailVoList = orderInfoVo.getOrderDetailVoList();
		List<OrderDerateVo> orderDerateVoList = orderInfoVo.getOrderDerateVoList();
		// 保存订单信息
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setUserId(userId);
		orderInfo.setOrderTitle(orderDetailVoList.get(0).getItemName());
		orderInfo.setOrderNo(orderInfoVo.getTradeNo());
		orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_UNPAID);
		orderInfo.setOriginalAmount(orderInfoVo.getOriginalAmount());
		orderInfo.setDerateAmount(orderInfoVo.getDerateAmount());
		orderInfo.setOrderAmount(orderInfoVo.getOrderAmount());
		orderInfo.setItemType(orderInfoVo.getItemType());
		orderInfo.setPayWay(orderInfoVo.getPayWay());
		orderInfoService.save(orderInfo);
		// 保存订单明细
		if (!CollectionUtils.isEmpty(orderDetailVoList))
			orderDetailService.saveBatch(orderDetailVoList
					.stream()
					.map(orderDetailVo -> {
						OrderDetail orderDetail = new OrderDetail();
						orderDetail.setOrderId(orderInfo.getId());
						orderDetail.setItemId(orderDetailVo.getItemId());
						orderDetail.setItemName(orderDetailVo.getItemName());
						orderDetail.setItemUrl(orderDetailVo.getItemUrl());
						orderDetail.setItemPrice(orderDetailVo.getItemPrice());
						return orderDetail;
					})
					.collect(Collectors.toList()));
		// 保存订单折扣
		if (!CollectionUtils.isEmpty(orderDerateVoList))
			orderDerateService.saveBatch(orderDerateVoList
					.stream()
					.map(orderDerateVo -> {
						OrderDerate orderDerate = new OrderDerate();
						orderDerate.setOrderId(orderInfo.getId());
						orderDerate.setDerateType(orderDerateVo.getDerateType());
						orderDerate.setDerateAmount(orderDerateVo.getDerateAmount());
						orderDerate.setRemarks(orderDerateVo.getRemarks());
						return orderDerate;
					})
					.collect(Collectors.toList()));

	// 	开启后续方法调用
		this.saveOrderAfterProcess(orderInfoVo.getTradeNo());
	}


	 void saveOrderAfterProcess(String orderNo);
}
